﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.DirectoryServices.AccountManagement;

using System.Configuration;
namespace ActiveDirectoryAuth.Account
{
    public partial class UserProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                
               /* PrincipalContext ctx = new PrincipalContext(ContextType.Domain,
                                            User.Identity.Name,
                                            ConfigurationSettings.AppSettings["container"].ToString());
                */
                PrincipalContext ctx = new PrincipalContext(ContextType.Domain, ConfigurationSettings.AppSettings["domain"].ToString(), ConfigurationSettings.AppSettings["container"].ToString()
                            , ConfigurationSettings.AppSettings["Admin_User_Name"].ToString(), ConfigurationSettings.AppSettings["Admin_User_Password"].ToString());


                
                
                
                UserPrincipal usr = new UserPrincipal(ctx);
                
                
                // Create a PrincipalSearcher object.
                PrincipalSearcher ps = new PrincipalSearcher(usr);
                PrincipalSearchResult<Principal> fr = ps.FindAll();
                foreach (UserPrincipal u in fr)
                {
                    if (u.Name == User.Identity.Name)
                    {
                        txtLoginName.Text = u.Name;
                        txtGivenName.Text = u.GivenName;
                        txtLastName.Text = u.Surname;
                        txtSamAccountName.Text = u.SamAccountName;
                        txtEmailAddress.Text = u.EmailAddress;
                        
                        txtContactnumber.Text = u.VoiceTelephoneNumber;
                    }
                }


                

                


            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            PrincipalContext ctx = new PrincipalContext(ContextType.Domain, ConfigurationSettings.AppSettings["domain"].ToString(), ConfigurationSettings.AppSettings["container"].ToString()
                            , ConfigurationSettings.AppSettings["Admin_User_Name"].ToString(), ConfigurationSettings.AppSettings["Admin_User_Password"].ToString());





            UserPrincipal usr = new UserPrincipal(ctx);


            // Create a PrincipalSearcher object.
            PrincipalSearcher ps = new PrincipalSearcher(usr);
            PrincipalSearchResult<Principal> fr = ps.FindAll();
            foreach (UserPrincipal u in fr)
            {
                if (u.Name == User.Identity.Name)
                {
                    u.GivenName = txtGivenName.Text;
                    u.Surname = txtLastName.Text;
                    u.SamAccountName = txtSamAccountName.Text;
                    u.VoiceTelephoneNumber = txtContactnumber.Text;
                    u.EmailAddress = txtEmailAddress.Text;
                    u.Save();
                    lblMessage.Text = "Updated";
                }
            }
            
        }
    }
}